package com.grts.mall.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.grts.mall.mapper.OrderMapper;
import com.grts.mall.pojo.Order;
import com.grts.mall.pojo.OrderExample;
import com.grts.mall.pojo.OrderExample.Criteria;
import com.grts.mall.pojo.vo.HomeResultVo;
import com.grts.mall.pojo.vo.OrderSearchConditionVO;

/**
 * @author Administrator
 *
 */
@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private OrderMapper orderMapper;

	@Override
	public List<Order> findAll(OrderSearchConditionVO vo) {
		OrderExample example = new OrderExample();
		example.createCriteria();
		List<Order> lists = this.orderMapper.selectPageList(vo);
		return lists;
	}

	@Override
	public long countByExaple(OrderSearchConditionVO vo) {
		OrderExample example = new OrderExample();
		Criteria criteria = example.createCriteria();
		if(vo.getOid()!=null) {
			criteria.andUidEqualTo(vo.getOid());
		}
		if (vo.getState()!=null) {
			criteria.andStateEqualTo(vo.getState());
		}
		long count = this.orderMapper.countByExample(example);
		return count;
	}

	@Override
	public List<HomeResultVo> findByDay() {
		List<HomeResultVo> vo = new ArrayList<HomeResultVo>();
		SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
		Date now = new Date();
		now.setDate(now.getDate()-8);
		for (int i = 0; i < 7; i++) {
			now.setDate(now.getDate()+1);
			String newdate = date.format(now);
			HomeResultVo vos = new HomeResultVo();
			vos.setCount(0);
			vos.setDate(newdate);
			vo.add(vos);
		}
		List<HomeResultVo> orders = this.orderMapper.selectVos();
		for (HomeResultVo homeResultVo : orders) {
			for (HomeResultVo homeResultVo2 : vo) {
				if (homeResultVo2.getDate().equals(homeResultVo.getDate())) {
					homeResultVo2.setCount(homeResultVo.getCount());
				}
			}
		}
		
		return vo;
	}
	@SuppressWarnings("deprecation")
	@Override
		public List<HomeResultVo> findBytime(String date) throws ParseException {
		String[] split = date.split("00:00:00");
		String substring = split[1].replace("- ", "");
		split[1] = substring;
		SimpleDateFormat dates = new SimpleDateFormat("yyyy-MM-dd");
		List<Date> dates2 = new ArrayList<Date>();
		for (String string : split) {
			Date now = dates.parse(string);
			dates2.add(now);
		}
		int a = dates2.get(1).getDate();
		int b = dates2.get(0).getDate();
		int c = a - b;
		List<HomeResultVo> vos = new ArrayList<HomeResultVo>();
		HomeResultVo home = new HomeResultVo();
		home.setDate(split[0]);
		vos.add(home);
		for (int i = 0; i < c; i++) {
			dates2.get(0).setDate(dates2.get(0).getDate()+1);
			home.setDate(dates.format(dates2.get(0)));
			home.setCount(0);
			vos.add(home);
			System.out.println(vos);
		}
			return null;
		}
}
